<?php
require_once 'SystemObject.php';
require_once 'db.php';

/**
 * Description of DataBaseObject
 *
 * @author josema
 */
class DataBaseObject extends SystemObject{
    protected $table;
    private $link;
    protected $key = 'id';

    protected $foreign_keys = null;

    /**
     * Construye el objeto que se encargará de realizar las conexiones con la
     * base de datos y lo almacena en la propiedad link del modelo.
     */
    public function  __construct($row = array(), $properties = false) {
        $this->init($row, $properties);
    }

    private function init($row = array(), $properties = false){
        foreach ($row as $key => $value) {
            if($properties) {
                if(in_array($key, $properties))
                    $this->{$key} = $value;
            }
            else
                $this->{$key} = $value;
        }
    }

    /**
     * GETTERS
     */

    /**
     * Returns the table name from this object.
     * @return string
     */
    public function getTable(){ return $this->table;}

    public function getKeyValue(){ return $this->{$this->key}; }

    public function load($key_value){
        $a = new ddbb(null, $this->debug);
        $a->connect(false);

        if(!$key_value) $key_value = $this->getKeyValue ();
        $resource = $a->select($this->table, array('*'), $this->key.' = '.$key_value);

        $row = $a->fetch($resource);

        $this->init($row);

        $a->close();
    }

    public function save(){
        if(!$this->{$this->key})
            return $this->insert();
        return $this->update();
    }

    public function delete(){
        $a = new ddbb(null, $this->debug);
        $a->connect(false);

        echo $a->delete($this->table, $this->key . ' = ' . $this->{$this->key});

        $a->close();
    }

    private function insert(){
        $a = new ddbb(null, $this->debug);
        $a->connect(false);

        $ret = $a->insert($this->table, $this->getAllPropertyNames(),$this->getAllProperties());

        $a->close();

        return $ret;
    }

    private function update(){
        $a = new ddbb(null, $this->debug);
        $a->connect(false);

        $a->update($this->table, $this->getAllPropertyNames(),
                $this->getAllProperties(), $this->key . ' = ' . $this->{$this->key});

        $a->close();
    }

    public function getRelation($relation_name){
        if (key_exists($relation_name, $this->foreign_keys)) {
            $rel = $this->foreign_keys[$relation_name];
            require_once Path::getPath($rel['path']) . $rel['class'] . '.php';

            $rel_class = new $rel['class'];

            $dbf = new DataBaseFactory($rel_class);

            if($rel['cardinality'] == 1){
                $dbf->selectWhere(array('*'), $rel_class->key . ' = ' . $this->$relation_name,
                        array($rel['cardinality']));
                if($row = $dbf->fetch())
                    $result = new $rel['class']($row);
            }
            //@todo falta por implementear
            else {
                $result = array();
                $dbf->selectWhere(array('*'), $rel['foreign_key'] . ' = ' . $this->{$this->key});
                while($row = $dbf->fetch())
                    $result[] = new $rel['class']($row);;
            }

            return $result;
        }
    }
}
?>
